<?

if (!$user)
{
  include "page_personal_logon.php";
  return;
}

$PageTitle=$msg["Page_ChangeUserPicture"];

include "div_menu_personal.php";


if (!$_POST) 
{
  if ($user["image"])
  {
    make_form1();
    print "<br>\n";
  }
  else
  {
    print "No image<br>\n";
    print "<br>\n";
  }
  make_form2();
}
else
{
  if (isset($_POST["delete"]))
  {
    delete_picture();
    $_SESSION["user"] = db_read_user_by_id($user["id"]);
    print_continue($msg["ImageDeleted"], "personal");
  }
  else
  {
    if (change_picture()) 
    {
      $_SESSION["user"] = db_read_user_by_id($user["id"]);
      print_continue($msg["ImageChanged"], "personal");
    } 
    else 
    {
      make_form2();
    };
  };
};


//-----------------------------------------------------------------------------

function make_form1() 
{
  global $msg;
  global $user;

  $size = db_read_rec( sprintf("SELECT width, height FROM images WHERE id=%d", $user["image"]) );

  $maxheight = ($size["height"] > 300) ? 300 : $size["height"];
?>

<!-- Delete picture form -->

<div class="form_area">
<div class="form_div">
<form enctype="multipart/form-data" action="personal_change_picture" method="post">
  <fieldset class="fields1">
  <dl>
    <img src="getimage?id=<?=$user["image"];?>" height=<?=$maxheight;?>>
    <br>
    Image size: <?=$size["width"];?> x <?=$size["height"];?>
    <br><br>
  </dl>

  <dl class="button_div">
    <button type="submit" tabindex="1" name="delete"><?=$msg["Delete"];?></button>
  </dl>
  </fieldset>
</form>
</div>
</div>

<?php
};


function make_form2() 
{
  global $msg;
  global $user;
?>

<!-- Upload picture form -->

<div class="form_area">
<div class="form_div">
<form enctype="multipart/form-data" action="personal_change_picture" method="post">
  <fieldset class="fields1">

  <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />

  <dl>
    <dt><label for="uploadedfile"><?=$msg["ChooseImageFile"];?></label></dt>
    <dd><input type="file" size="80" tabindex="1" name="uploadedfile" id="uploadedfile" value="<?=maskqoute($_FILES['uploadedfile']['name']);?>"/></dd>
  </dl>

  <dl class="button_div">
    <button type="submit" tabindex="2" name="save"><?=$msg["Change"];?></button>
    &nbsp;
    <button type="button" tabindex="3" name="cancel" onClick="window.location.href='personal'"><?=$msg["Cancel"];?></button>
  </dl>

  </fieldset>
</form>
</div>
</div>

<?php
};


function change_picture() 
{
  global $msg;
  global $user;

  $n = 0;

  try 
  {
    db_call();

    $error    = $_FILES['uploadedfile']['error'];
    $src_name = $_FILES['uploadedfile']['name'];
    $src_size = $_FILES['uploadedfile']['size'];
    $tmp_name = $_FILES['uploadedfile']['tmp_name'];
//  Echo "Src=$src_name, Size=$src_size, Tmp=$tmp_name, Error=$error<br>";
 
    if ($error <> 0)
    {
      $errors[$n++] = sprintf($msg["UploadError"], $error);
    }

    if ($n == 0) 
    {
      $dst_name = "uploads/".basename($src_name); 

      if (!move_uploaded_file($tmp_name, $dst_name)) 
      {
        $errors[$n++] = $msg["FileError"];
      }

      $size = getimagesize($dst_name);
//    echo "X=$size[0], Y=$size[1]<br>";
      if (!$size)
      {
        $errors[$n++] = $msg["BadImage"];
      }

      if ($n == 0) 
      {
        db_change_user_picture($user["id"], $dst_name, $size);
        unlink($dst_name);
      }
    }
  } 
  catch (Exception $e) 
  {
    $errors[$n++] = $e->getMessage();
  }


  if ($n <> 0) 
  {
    print_errors($errors);
    return False;
  }

  return True;
};


function delete_picture() 
{
  global $msg;
  global $user;

  $n = 0;

  try 
  {
    db_call();
    db_change_user_picture($user["id"], "", 0);
  } 
  catch (Exception $e) 
  {
    $errors[$n++] = $e->getMessage();
  }


  if ($n <> 0) 
  {
    print_errors($errors);
    return False;
  }

  return True;
};

?>
